package com.lh.music.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.lh.music.IMusic.NetMusic;
import com.lh.music.PlayApplication;
import com.lh.music.api.Api;
import com.lh.music.constant.Constants;
import com.lh.music.entity.Bitrate;
import com.lh.music.entity.NetSong;

import java.io.IOException;
import java.util.List;

public class PlayNetService extends Service {
    private PlayApplication app;
    MediaPlayer player;
    public PlayNetService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app= (PlayApplication) getApplication();
        System.out.println("服务已经被创建");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        PlayNetBinder binder=new PlayNetBinder();
        app.netMusic=binder;
        return binder;
    }
    class PlayNetBinder extends Binder implements NetMusic{

        @Override
        public void initNetMusic(List<NetSong> netSongList,int position) {
            app.currentPosition=position;
        }

        @Override
        public void startNetPlay() {
            //更改全局变量的值
            app.songState=Constants.PLAYING;
            app.currentType= Constants.NET;
            app.currentNetSong=app.currentNetSongList.get(app.currentPosition);
            String id="";
            //排行中的id
            if ( app.currentNetSong.getSong_id()!=null){
                id=  app.currentNetSong.getSong_id();
            }
            //电台中的id
            if (app.currentNetSong.getSongid()!=null){
                id=app.currentNetSong.getSongid();
            }
            RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
            StringRequest request=new StringRequest(Request.Method.GET,
                    Api.SONG_PLAY_API + id,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            JSONObject job= JSON.parseObject(response);
                            //没有返回正确的歌曲信息
                            if (job.getString("error_code").equals("22469")){
                                Toast.makeText(app, "暂无播放路径", Toast.LENGTH_SHORT).show();
                            }else {
                                JSONObject bitrate=job.getJSONObject("bitrate");
                                Bitrate b=JSON.parseObject(bitrate.toJSONString(),Bitrate.class);
                                app.currentNetSong.setBitrate(b);
                                player=new MediaPlayer();
                                try {
                                    player.setDataSource(b.getFile_link());
                                    player.prepareAsync();
                                    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                        @Override
                                        public void onPrepared(MediaPlayer mp) {
                                            player.start();
                                        }
                                    });
                                    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                        @Override
                                        public void onCompletion(MediaPlayer mp) {
                                            nextNetPlay();
                                        }
                                    });
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
            queue.add(request);
        }

        @Override
        public void stopNetPlay() {
            if (player!=null){
                player.stop();
                player.release();
                player=null;
            }
        }

        @Override
        public void pauseNetPlay() {
            if (player!=null&&player.isPlaying()){
                player.pause();
            }
            app.songState=Constants.PAUSED;
        }

        @Override
        public void continueNetPlay() {
            if (player!=null&&!player.isPlaying()){
                player.start();
            }
            app.songState=Constants.PLAYING;
        }

        @Override
        public void nextNetPlay() {
            //如果当前位置为最后一个 将位置设置为第一首歌
            if (app.currentPosition==app.currentNetSongList.size()-1){
                app.currentPosition=0;
            }else{
                app.currentPosition++;
            }
            stopNetPlay();
            startNetPlay();
        }

        @Override
        public void prevNetPlay() {
            //如果当前位置为第一个  将位置设置为最后一首
            if (app.currentPosition==0){
                app.currentPosition=app.currentNetSongList.size()-1;
            }else {
                app.currentPosition--;
            }
            stopNetPlay();
            startNetPlay();
        }

        @Override
        public int getCurrentProgress() {
            if (player!=null){
                return player.getCurrentPosition();
            }
            return 0;
        }
    }
}
